package lj.courseinterect.dao;

import lj.courseinterect.dao.i.StudentCourseDao;
import lj.courseinterect.dao.i.StudentExamDao;
import lj.courseinterect.entity.StudentCourse;
import lj.courseinterect.entity.StudentExam;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Root;
import java.util.List;

public class JpaStudentCourseDao extends JpaDao<StudentCourse, Long> implements StudentCourseDao{

    public JpaStudentCourseDao() {
        super(StudentCourse.class);
    }


    
    @Override
    @Transactional(readOnly = true)
    public List<StudentCourse> findByCouId(Long couId) {
        final CriteriaBuilder builder = this.getEntityManager().getCriteriaBuilder();
        final CriteriaQuery<StudentCourse> criteriaQuery = builder.createQuery(this.entityClass);

        Root<StudentCourse> root = criteriaQuery.from(this.entityClass);
        Path<String> namePath = root.get("couId");
        criteriaQuery.where(builder.equal(namePath, couId));


        TypedQuery<StudentCourse> typedQuery = this.getEntityManager().createQuery(criteriaQuery);
        List<StudentCourse> studentCourses= typedQuery.getResultList();

        if (studentCourses.isEmpty()) {
            return null;
        }
        return studentCourses;
    }

    @Override
    public List<StudentCourse> findByStuId(Long stuId) {
        final CriteriaBuilder builder = this.getEntityManager().getCriteriaBuilder();
        final CriteriaQuery<StudentCourse> criteriaQuery = builder.createQuery(this.entityClass);

        Root<StudentCourse> root = criteriaQuery.from(this.entityClass);
        Path<String> namePath = root.get("stuId");
        criteriaQuery.where(builder.equal(namePath, stuId));


        TypedQuery<StudentCourse> typedQuery = this.getEntityManager().createQuery(criteriaQuery);
        List<StudentCourse> studentCourses= typedQuery.getResultList();

        if (studentCourses.isEmpty()) {
            return null;
        }
        return studentCourses;
    }

    @Override
    public StudentCourse findByStuIdAndCouId(Long stuId, Long couId) {
        final CriteriaBuilder builder = this.getEntityManager().getCriteriaBuilder();
        final CriteriaQuery<StudentCourse> criteriaQuery = builder.createQuery(this.entityClass);

        Root<StudentCourse> root = criteriaQuery.from(this.entityClass);
        Path<String> stuPath = root.get("stuId");
        Path<String> couPath = root.get("couId");
        criteriaQuery.where(builder.and(builder.equal(stuPath, stuId), builder.equal(couPath, couId)));

        TypedQuery<StudentCourse> typedQuery = this.getEntityManager().createQuery(criteriaQuery);
        List<StudentCourse> studentCourses= typedQuery.getResultList();

        if (studentCourses.isEmpty()) {
            return null;
        }
        return studentCourses.iterator().next();
    }
}
